
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var PORT = 3000;
var count = 0;
app.listen(PORT);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.on('connection', function (socket) {
    count++;
    socket.nickname = "user" + count;
    io.emit('enter',socket.nickname + 'comes in') 
    socket.on('message',function(str){
        io.emit('message',socket.nickname + 'sya' + str) 
    })
    socket.on('disconnect',function(str){
        io.emit('leave',socket.nickname + 'left') 
    })
});